home *** CD-ROM | disk | FTP | other *** search
- { flow.pas -- Display flowing animation }
-
- program Flow;
-
- uses WinTypes, WinProcs, WObjects;
-
- const
-
- timer_ID = 1; { Local timer id number }
- max_Index = 100; { Maximum number of lines visible }
-
- dx1: Integer = 4; { "Delta" values for controlling }
- dy1: Integer = 10; { the animation's personality. }
- dx2: Integer = 3;
- dy2: Integer = 9;
-
- type
-
- LineRec = record
- X1, Y1, X2, Y2 : Integer;
- Color: TColorRef;
- end;
-
- FlowApplication = object(TApplication)
- procedure InitMainWindow; virtual;
- end;
-
- PFlowWindow = ^FlowWindow;
- FlowWindow = object(TWindow)
- Dc: HDC;
- procedure SetupWindow;
- virtual;
- procedure WMDestroy(var Msg: TMessage);
- virtual wm_First + wm_Destroy;
- procedure WMTimer(var Msg: TMessage);
- virtual wm_First + wm_Timer;
- procedure Paint(PaintDC: HDC; var PaintInfo: TPaintStruct);
- virtual;
- end;
-
- var
-
- LineArray: array[0 .. max_Index - 1] of LineRec;
- Index: Integer; { Index for LineArray }
- Erasing: Boolean; { True if erasing old lines }
-
-
- {- Return -1 if n < 0 or +1 if n >= 0 }
- function Sign(n: Integer): Integer;
- begin
- if n < 0 then Sign := -1 else Sign := 1
- end;
-
- {- Create new line, direction, and color }
- procedure MakeNewLine(Dc: HDC; R: TRect; Index: Integer);
-
- procedure NewCoord(var C, Change: Integer; Max: Integer;
- var color: TColorRef);
- var
- Temp: Integer;
- begin
- Temp := C + Change;
- if (Temp < 0) or (Temp > Max) then
- begin
- Change := Sign(-Change) * (3 + Random(12));
- repeat
- color := RGB(Random(256), Random(256), Random(256));
- color := GetNearestColor(Dc, color)
- until color <> GetBkColor(Dc)
- end else
- C := Temp
- end;
-
- begin
- with LineArray[Index] do
- begin
- NewCoord(x1, dx1, R.right, color);
- NewCoord(y1, dy1, R.bottom, color);
- NewCoord(x2, dx2, R.right, color);
- NewCoord(y2, dy2, R.bottom, color)
- end
- end;
-
- {- Draw or erase a line identified by Index }
- procedure DrawLine(Dc: HDC; Index: Integer);
- var
- OldPen, Pen: HPen;
- OldROP: Integer;
- begin
- with LineArray[Index] do
- begin
- Pen := CreatePen(ps_Solid, 1, color);
- OldPen := SelectObject(Dc, Pen);
- OldROP := SetROP2(Dc, r2_XorPen);
- MoveTo(Dc, x1, y1);
- LineTo(Dc, x2, y2);
- SelectObject(Dc, OldPen);
- DeleteObject(Pen);
- SetROP2(Dc, OldROP)
- end
- end;
-
- { FlowApplication }
-
- {- Initialize the application's window }
- procedure FlowApplication.InitMainWindow;
- var
- I: Integer;
- begin
- MainWindow := New(PFlowWindow, Init(nil, 'Go with the Flow'));
- Randomize;
- index := 0;
- erasing := False;
-
- {- Fill all x1 fields in LineArray with -1 values so the Paint
- method will redraw only valid lines. }
-
- for I := 0 to max_Index - 1 do
- LineArray[I].x1 := -1
-
- end;
-
- { FlowWindow }
-
- {- Initialize the window's actions }
- procedure FlowWindow.SetupWindow;
- begin
- TWindow.SetupWindow;
- SetTimer(hWindow, timer_ID, 1, nil)
- end;
-
- {- Intercept wm_Destroy message }
- procedure FlowWindow.WMDestroy(var Msg: TMessage);
- begin
- KillTimer(hWindow, timer_ID);
- TWindow.WMDestroy(Msg)
- end;
-
- {- Execute one "tick" of the animation }
- procedure FlowWindow.WMTimer(var Msg: TMessage);
- var
- R: TRect;
- S: LineRec;
- I, OldIndex: Integer;
- begin
- Dc := GetDC(hWindow);
- GetClientRect(hWindow, R);
- for I := 1 to 10 do
- begin
- OldIndex := Index;
- if Index = max_Index - 1 then
- begin
- Index := 0;
- Erasing := True
- end else
- inc(Index);
- if Erasing then DrawLine(Dc, Index);
- {- Set new lines to begin where old lines end }
- LineArray[Index] := LineArray[OldIndex];
- MakeNewLine(Dc, R, Index);
- DrawLine(Dc, Index)
- end;
- ReleaseDC(hWindow, Dc)
- end;
-
- {- Repaint graphics in window }
- procedure FlowWindow.Paint(PaintDC: HDC; var PaintInfo:
- TPaintStruct);
- var
- I: Integer;
- R: TRect;
- begin
- GetClientRect(HWindow, R);
- FillRect(PaintDC, R, GetStockObject(black_Brush));
- for I := 0 to max_Index - 1 do
- if LineArray[I].x1 >= 0 then DrawLine(PaintDC, I)
- end;
-
- var
-
- FlowApp: FlowApplication;
-
- begin
- FlowApp.Init('FlowApp');
- FlowApp.Run;
- FlowApp.Done
- end.
-
-
- {--------------------------------------------------------------
- Copyright (c) 1991 by Tom Swan. All rights reserved.
- Revision 1.00 Date: 2/20/1991
- ---------------------------------------------------------------}
-